﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GrainManage.Entities.Account;
using GrainManage.Proxy;
using GrainManage.Web.Common;
using GrainManage.Entities.Encrypt;
using Resources;

namespace GrainManage.Web.Account
{
    public partial class Login : BasePage
    {
        private const string CookieName = "GrainManage";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Utils.GetCookie(CookieName)))
                {
                    var isChecked = Convert.ToBoolean(Utils.GetCookie(CookieName, "RememberMe"));
                    if (isChecked)
                    {
                        cbRememberId.Checked = isChecked;
                        txtUserName.Text = DESEncrypt.Decrypt(Utils.GetCookie(CookieName, "AdminName"));
                        txtUserPwd.Text = DESEncrypt.Decrypt(Utils.GetCookie(CookieName, "AdminPwd"));
                    }
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string userName = txtUserName.Text.Trim();
            string userPwd = txtUserPwd.Text.Trim();
            string code = txtCode.Text.Trim();
            if (userName.Equals("") || userPwd.Equals(""))
            {
                ShowError(ErrorResource.NamePwdNeeded);
                return;
            }
            if (code.Equals(""))
            {
                ShowError(ErrorResource.VericationNeeded);
                return;
            }
            if (Session[DTKeys.SESSION_CODE] == null)
            {
                ShowError(ErrorResource.VericationNotFound);
                return;
            }
            if (code.ToLower() != Session[DTKeys.SESSION_CODE].ToString().ToLower())
            {
                ShowError(ErrorResource.VericationNotMatch);
                return;
            }

            bool isSucceed = false;
            string errorMessage = string.Empty;

            var accountBusiness = new AccountProxy();
            int encryptModel = (int)GrainManage.Entities.Encrypt.EncryptModel.RSA;
            var key = accountBusiness.GetEncryptKey(new InputGetEncryptKey { AccountName = txtUserName.Text, EncryptModel = encryptModel });
            if (!string.IsNullOrEmpty(key.EncryptKey))
            {
                var input = new InputSignIn();
                input.UserName = txtUserName.Text;
                input.Password = GrainManage.Encrypt.RSAEncrypt.Encrypt(txtUserPwd.Text, key.EncryptKey);
                input.EncryptModel = encryptModel;
                var result = accountBusiness.SignIn(input);
                if (result.Status.StatusCode == 1)
                {
                    isSucceed = true;
                    //写入Cookies
                    if (cbRememberId.Checked)
                    {
                        Utils.WriteCookie(CookieName, "RememberMe", Convert.ToString(cbRememberId.Checked), 14400);
                        Utils.WriteCookie(CookieName, "AdminName", DESEncrypt.Encrypt(userName));
                        Utils.WriteCookie(CookieName, "AdminPwd", DESEncrypt.Encrypt(userPwd));
                        Response.Redirect("../Index.aspx");
                    }
                    else
                    {
                        Utils.WriteCookie(CookieName, Convert.ToString(cbRememberId.Checked), -14400);
                    }
                }
                else
                {
                    errorMessage = result.Status.Message;
                }
            }
            else
            {
                errorMessage = key.Status.Message;
            }

            if (!isSucceed)
            {
                ShowError(errorMessage);
                return;
            }
        }

        private void ShowError(string message)
        {
            lblTip.Visible = true;
            lblTip.Text = message;
        }
    }
}